package com.zjw.dao.impl;

import com.zjw.dao.BaseDao;
import com.zjw.entity.Goods;
import com.zjw.dao.GoodsDao;
import com.zjw.utils.ConnectionUtil;
import com.zjw.utils.Constants;
import com.zjw.vo.GoodsDetail;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 好物信息表(Goods)表数据库访问层实现类
 *
 * @author 翟
 * @since 2025-04-09 09:21:58
 */
public class GoodsDaoImpl extends BaseDao implements GoodsDao {

    public GoodsDaoImpl(Connection connection) {
        super(connection);
    }

    @Override
    public List<Goods> selectGoodsByLoginTypeAndPaging(int index, int pageSize, String loginType) throws SQLException {
        /**
         *select goods_id , goods_name , goods_price , goods_add_time from goods
         * where isOnSell = 1 limit 0 , 5
         */
        String sql = "select goods_id , goods_name , goods_price , goods_add_time from goods";
        List<Object> list = new ArrayList<>();
        List<Goods> goodsList = new ArrayList<>();
        if (loginType.equals(Constants.LOGIN_TYPE_USERNAME)){
            sql += " where isOnSell = ?";
            list.add(Constants.GOODS_STATUS_ON_SALE);
        }
        sql += " limit ? , ?";
        list.add(index);
        list.add(pageSize);
        ResultSet resultSet = null;
        try {
            resultSet = executeQuery(sql , list.toArray());
            while (resultSet.next()){
                Goods goods = new Goods();
                goods.setGoodsId(resultSet.getInt("goods_id"));
                goods.setGoodsName(resultSet.getString("goods_name"));
                goods.setGoodsPrice(resultSet.getDouble("goods_price"));
                goods.setGoodsAddTime(resultSet.getTimestamp("goods_add_time"));
                goodsList.add(goods);
            }
            return goodsList;
        } finally {
            ConnectionUtil.closeALL(resultSet , null , null);
        }
    }

    @Override
    public int selectGoodsCountByLoginType(String loginType) throws SQLException {
        String sql = "select count(1) from goods";
        List<Object> list = new ArrayList<>();
        ResultSet resultSet = null;
        if (loginType.equals(Constants.LOGIN_TYPE_USERNAME)){
            sql += " where isOnSell = ?";
            list.add(Constants.GOODS_STATUS_ON_SALE);
        }
        try {
            resultSet = executeQuery(sql , list.toArray());
            if (resultSet.next()){
                return resultSet.getInt(1);
            }
        } finally {
            ConnectionUtil.closeALL(resultSet , null , null);
        }
        return 0;
    }

    @Override
    public GoodsDetail selectGoodsDetailByGoodsId(int goodsId  , String loginType) throws SQLException {
        List<Object> list = new ArrayList<>();
        list.add(goodsId);
        String sql = "select goods_id , seller_id , goods_name , type_name , goods_price , user_name , goods_introduce , goods_add_time from goods\n" +
                "inner join type t on goods.type_id = t.type_id\n" +
                "inner join user u on goods.seller_id = u.user_id\n" +
                "where goods_id = ?\n";
        if (loginType.equals(Constants.LOGIN_TYPE_USERNAME)) {
            sql += "and isOnSell = ?";
            list.add(Constants.GOODS_STATUS_ON_SALE);
        }
        ResultSet resultSet = null;
        try {
            resultSet = executeQuery(sql , list.toArray());
            if (resultSet.next()){
                GoodsDetail goodsDetail = new GoodsDetail();
                goodsDetail.setGoodsId(resultSet.getInt("goods_id"));
                goodsDetail.setSellerId(resultSet.getInt("seller_id"));
                goodsDetail.setGoodsName(resultSet.getString("goods_name"));
                goodsDetail.setTypeName(resultSet.getString("type_name"));
                goodsDetail.setGoodsPrice(resultSet.getDouble("goods_price"));
                goodsDetail.setUserName(resultSet.getString("user_name"));
                goodsDetail.setGoodsIntroduce(resultSet.getString("goods_introduce"));
                goodsDetail.setGoodsAddTime(resultSet.getTimestamp("goods_add_time"));
                return goodsDetail;
            }
        } finally {
            ConnectionUtil.closeALL(resultSet , null , null);
        }
        return null;
    }

    @Override
    public int updateIsOnSellByGoodsId(int goodsId) throws SQLException {
        String sql = "update goods set is_on_sell = ? where goods_id = ?";
        return executeUpdate(sql , Constants.GOODS_STATUS_SOLD_OUT , goodsId);
    }

    @Override
    public int insertGoods(Goods goods) throws SQLException {
        String sql = "insert into goods(goods_id , goods_name , goods_price , goods_introduce , goods_add_time , isOnSell , type_id , seller_id) values(null , ? , ? , ? , ? , ? , ? , ?)";
        return executeUpdate(sql , goods.getGoodsName() , goods.getGoodsPrice() , goods.getGoodsIntroduce() ,
                goods.getGoodsAddTime() , goods.getisOnSell(), goods.getTypeId() , goods.getSellerId());
    }

    @Override
    public List<Goods> selectGoodsByUserIdAndPaging(int index, int pageSize, int user_id) throws SQLException {
        String sql = "select goods_id , goods_name , (select type_name from type where type.type_id = goods.type_id) typeName , goods_add_time , goods_introduce from goods where seller_id = ? limit ? , ?";
        ResultSet resultSet = null;
        List<Goods> goodsList = null;
        try {
            resultSet = executeQuery(sql , user_id , index , pageSize);
             goodsList = new ArrayList<>();
            while (resultSet.next()){
                Goods goods = new Goods();
                goods.setGoodsId(resultSet.getInt("goods_id"));
                goods.setGoodsName(resultSet.getString("goods_name"));
                goods.setTypeName(resultSet.getString("typeName"));
                goods.setGoodsAddTime(resultSet.getDate("goods_add_time"));
                goods.setGoodsIntroduce(resultSet.getString("goods_introduce"));
                goodsList.add(goods);
            }
        } finally {
            ConnectionUtil.closeALL(resultSet , null , null);
        }
        return goodsList;
    }

    @Override
    public int selectGoodsCountByUserId(int user_id) throws SQLException {
        String sql = "select count(1) from goods where seller_id = ?";
        ResultSet resultSet = null;
        try {
            resultSet = executeQuery(sql , user_id);
            if (resultSet.next()){
                return resultSet.getInt(1);
            }
        } finally {
            ConnectionUtil.closeALL(resultSet , null , null);
        }
        return 0;
    }
}
